from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from tortoise.contrib.fastapi import register_tortoise

# from app.page.xx import wietn_app
from tool.sessing import TORTOISE_ORM
from fastapi import APIRouter
from fastapi.responses import RedirectResponse

# 导入用户路由
from app.user.user_router import user_router

# 导入用户端页面路由
from app.page.page_user import page_user
from app.writer.writer import writer_router

app = FastAPI(title="智能小说平台API", version="1.0.0")

# 配置CORS，允许前端访问
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:5173",
        "http://127.0.0.1:5173",
        "http://localhost:5176",
        "http://127.0.0.1:5176",
        "http://localhost:8000",
        "http://127.0.0.1:8000",
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)



@app.get("/")
async def root():
    return {"message": "Hello World"}



@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}


# 注册用户路由（包含钉钉登录功能）
app.include_router(user_router, prefix="/user", tags=["用户"])

# 注册用户端页面路由
app.include_router(page_user, prefix="/page", tags=["用户端"])
# 注册作者私有路由
app.include_router(writer_router, prefix="/writer", tags=["作者"])

# 注册所有API路由
api_router = APIRouter(prefix="/api")
app.include_router(api_router)


import uvicorn
# 注册Tortoise ORM
register_tortoise(
    app,
    config=TORTOISE_ORM,
    generate_schemas=True,
)

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)